//
//  MKTChatMsgLinksTVCellOther.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2021/7/15.
//  Copyright © 2021 moon. All rights reserved.
//


#import "MKTChatMsgLinksTVCellOther.h"

@interface MKTChatLinkTVCell_MsgLink : MKUITableViewCellBase
@property(nonatomic,strong) UILabel *titleLabel;
@property(nonatomic,strong) MKUIFlowLayoutConstraint *flowlayout;
@end
@implementation MKTChatLinkTVCell_MsgLink
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.numberOfLines = 0;
        self.titleLabel.textColor = [UIColor whiteColor];
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.titleLabel];
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        //
        self.flowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[self.titleLabel] constraintParam:(MKUIFlowLayoutConstraintParam_H_C_L) contentInsets:UIEdgeInsetsMake(5, 0, 5, 0) interitemSpacing:5];
    }
    return self;
}
- (void)customLayoutSubviews{
    [super customLayoutSubviews];
    CGRect bounds = self.contentView.bounds;
    self.flowlayout.bounds = bounds;
    [self.flowlayout layoutItemsWithResizeItems:YES];
}
- (CGSize)customSizeThatFits:(CGSize)size{
    CGSize s = [self.flowlayout sizeThatFits:size resizeItems:YES];
    s.height = MAX(s.height,30);
    return s;
}
- (void)customReloadCellModel{
    [super customReloadCellModel];
    MKTChatLink *modelValue = self.cellModel.modelValue;
    self.titleLabel.text = modelValue.title;
}
@end
#import "MKTChatBubbleView.h"

@interface MKTChatMsgLinksTVCellOther()
@property(nonatomic,strong) MKTChatBubbleView *bubbleView;
@property(nonatomic,strong) UILabel *titleLabel;
@property(nonatomic,strong) MKUIFlowLayoutButton *refreshButton;
@property(nonatomic,strong) UIView *lineView;
@property(nonatomic,strong) MKUITableView *tableView;

@property(nonatomic,strong) MKUIFlowLayoutConstraint *flowlayout;
@end

@implementation MKTChatMsgLinksTVCellOther
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.bubbleView = [[MKTChatBubbleView alloc] init];
        self.bubbleView.bgView.backgroundColor = [UIColor grayColor];
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.textColor = [UIColor whiteColor];
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.bubbleView addSubview:self.titleLabel];
        //
        self.refreshButton = [[MKUIFlowLayoutButton alloc] initWithContentStyle:(MKUIFlowLayoutButtonContentStyleHorizontal)];
        [self.refreshButton setTitle:@"换一批" forState:(UIControlStateNormal)];
        self.refreshButton.imageSize = CGSizeMake(16, 16);
        [self.refreshButton setImage:[UIImage imageNamed:@"app-icon-11.png"] forState:(UIControlStateNormal)];
        self.refreshButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.refreshButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        [self.refreshButton mk_addClickActionBlock:^(MKTChatMsgLinksTVCellOther *self) {
            MKTChatMsgLinks *modelValue = self.cellModel.modelValue;
            NSInteger pageindex = [self.cellModel mk_integerForKeyPath:@"pageindex" otherwise:0];
            NSInteger pagenum = modelValue.pagenum;
            NSInteger pagecount = ceil(1.0*modelValue.links.count/pagenum);
            pageindex = (pageindex+1)%pagecount;
            self.cellModel[@"pageindex"] = @(pageindex);
            [self.cellModel refresh];
        } context:self];
        [self.bubbleView addSubview:self.refreshButton];
        //
        MKUISegmentFlowLayoutConstraint *f1 = [[MKUISegmentFlowLayoutConstraint alloc] initWithItems:@[self.titleLabel,self.refreshButton] constraintParam:(MKUISegmentFlowLayoutConstraint_H_C) contentInsets:(UIEdgeInsetsZero) interitemSpacing:5];
        f1.isLayoutPriorityFirstItems = NO;
        f1.fixSizeToFitsBounds = YES;
        //
        self.lineView = [[UIView alloc] init];
        self.lineView.backgroundColor = [UIColor whiteColor];
        MKUILayoutConstraintItemWrapper *w1 = [MKUILayoutConstraintItemWrapper wrapItem:self.lineView sizeThatFitsBlock:^CGSize(MKUILayoutConstraintItemWrapper * _Nonnull wrapper, CGSize size, BOOL resizeItems) {
            return CGSizeMake(size.width, 1.0/[UIScreen mainScreen].scale);
        }];
        [self.bubbleView addSubview:self.lineView];
        //
        self.tableView = [[MKUITableView alloc] initWithFrame:self.bounds style:(UITableViewStylePlain)];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tableView.backgroundColor = [UIColor clearColor];
        [self.bubbleView addSubview:self.tableView];
        self.bubbleView.containerLayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[f1,w1,self.tableView] constraintParam:(MKUIFlowLayoutConstraintParam_V_T_C) contentInsets:[MKTChatBubbleView defaultContentInsets] interitemSpacing:5];
        [self.contentView addSubview:self.bubbleView];
        
        self.flowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[self.msgUserPicView,self.bubbleView] constraintParam:(MKUIFlowLayoutConstraintParam_H_T_L) contentInsets:[self.class contentInsets] interitemSpacing:5];
    }
    return self;
}
- (void)customLayoutSubviews{
    [super customLayoutSubviews];
    CGRect bounds = self.contentView.bounds;
    self.flowlayout.bounds = bounds;
    [self.flowlayout layoutItemsWithResizeItems:YES];
}
- (CGSize)customSizeThatFits:(CGSize)size{
    return [self.flowlayout sizeThatFits:size resizeItems:YES];
}
- (void)customReloadCellModel{
    [super customReloadCellModel];
    MKTChatMsgLinks *modelValue = self.cellModel.modelValue;
    self.titleLabel.text = modelValue.title;
    [self __reloadData];
}
- (void)__reloadData{
    [self.tableView.model removeAllSectionModels];
    MKTChatMsgLinks *modelValue = self.cellModel.modelValue;
    NSInteger pageindex = [self.cellModel mk_integerForKeyPath:@"pageindex" otherwise:0];
    NSInteger pagenum = modelValue.pagenum;
    for (int i=0; i<pagenum; i++) {
        NSInteger index = pageindex*pagenum+i;
        if(index>=modelValue.links.count){
            break;
        }
        MKTChatLink *link = modelValue.links[index];
        MKUITableViewCellModel *cm = [MKUITableViewCellModel modelWithValue:link cellClass:[MKTChatLinkTVCell_MsgLink class] whenClick:^(__kindof MKUITableViewCellModel * _Nonnull cellModel) {
            [cellModel.tableView deselectRowAtIndexPath:cellModel.indexPathInModel animated:YES];
            [link handleClick:cellModel.tableViewCell];
        }];
        [self.tableView.model addCellModel:cm];
    }
    [self.tableView.model reloadTableViewData];
}
@end
